package listbyorder.access401_500.test454;

import java.util.HashMap;

public class Solution3 {

    // 使用HashMap做缓存
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int len = A.length;
        int ans = 0;

        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                int curSum = A[i] + B[j];
                int counts = map.getOrDefault(curSum, 0);
                map.put(curSum, counts + 1);
            }
        }

        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                int curSum = -(C[i] + D[j]);
                if (map.containsKey(curSum)) {
                    ans += map.get(curSum);
                }
            }
        }

        return ans;
    }
}
